//
//  OSHomeVC.m
//  ZKPayApp
//
//  Created by 凯朱 on 2024/10/29.
//

#import "SJMeVC.h"
#import "SJMeHeadView.h"
#import "SJMeListCell.h"
#import "SJMeLogOutVC.h"
#import "SJMyApplyListVC.h"
static  NSString *const kCellId = @"SJMeListCell";
@interface SJMeVC ()<UITableViewDelegate,UITableViewDataSource>{
    
}
@property (nonatomic, strong)SJMeHeadView *headView;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArray;
@end



@implementation SJMeVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isHidenNaviBar = YES;
    [self configDatas];
    self.view.backgroundColor = kHexColor(@"#F7F9FF");
    [self.view addSubview:self.tableView];
    _headView = [SJMeHeadView viewFromXIB];
    _headView.frame = CGRectMake(0, 0, KScreenWidth,220);
    self.tableView.tableHeaderView = _headView;
    [self loginStatusChange];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginStatusChange) name:@"loginStatusChange" object:nil];
}

-(void)configDatas{
    NSArray *titles = @[kLocalString(@"我的借款"),kLocalString(@"帮助中心"),kLocalString(@"常见问题"),kLocalString(@"联系客服"),kLocalString(@"关于我们"),kLocalString(@"设置")];
    [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSMutableDictionary *dict = [NSMutableDictionary new];
        [dict setObject:obj forKey:@"title"];
        [self.dataArray addObject:dict];
    }];
}

-(void)loginStatusChange{
    if (kIsTourst) {
        _headView.nameLbl.text = kLocalString(@"未登录");
    }else{
        _headView.nameLbl.text = kUserInfo.userId;
    }
}

#pragma mark - ================<tableViewDataSource>===============
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SJMeListCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dict = _dataArray[indexPath.section];
    cell.titleLbl.text = dict[@"title"];
    return cell;
}

#pragma mark - ================<UITableViewDelegate>===============
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 59;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UITableViewHeaderFooterView *headview = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"head"];
    if (!headview) {
        headview = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"head"];
        headview.frame = CGRectMake(0, 0, kScreenWidth, 15);
        headview.contentView.backgroundColor = KClearColor;
    }
    return headview;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"foot"];
    if (!footerView) {
        footerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"foot"];
        footerView.frame = CGRectMake(0, 0, kScreenWidth, 0.1);
        footerView.contentView.backgroundColor = KClearColor;
    }
    return footerView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dict = _dataArray[indexPath.section];
    NSString *title = dict[@"title"];
    if ([title isEqualToString:kLocalString(@"设置")]) {
        SJMeLogOutVC *vc = [SJMeLogOutVC new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([title isEqualToString:kLocalString(@"联系客服")]){
        NSMutableString *telStr = [[NSMutableString alloc] initWithFormat:@"tel:18888888888"];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:telStr]];
    }else if ([title isEqualToString:kLocalString(@"关于我们")]){
        RootWebViewController *vc = [RootWebViewController new];
        vc.title = title;
        vc.url = @"https://www.baidu.com";
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([title isEqualToString:kLocalString(@"我的借款")]){
        SJMyApplyListVC *vc = [SJMyApplyListVC new];
        vc.title = title;
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([title isEqualToString:kLocalString(@"常见问题")]){
        RootWebViewController *vc = [RootWebViewController new];
        vc.title = title;
        vc.url = @"https://www.baidu.com";
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([title isEqualToString:kLocalString(@"帮助中心")]){
        RootWebViewController *vc = [RootWebViewController new];
        vc.title = title;
        vc.url = @"https://www.baidu.com";
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - ================<懒加载>===============
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,kScreenWidth,kScreenHeight-SafeAreaBottomHeight-50) style:UITableViewStyleGrouped];
        _tableView.estimatedRowHeight = 59;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:kCellId bundle:nil] forCellReuseIdentifier:kCellId];
        _tableView.backgroundColor = KClearColor;
    }
    return _tableView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}




@end
